package lc20240424;

import java.util.Arrays;

public class ArrayProduct {
    public static void main(String[] args) {
        int[] nums={1,2,3,4};
        System.out.println(Arrays.toString(productExceptSelf(nums)));

    }

    public static int[] productExceptSelf(int[] nums) {
        int n= nums.length;
        int[] res=new int[nums.length];
        //左边的乘积
        res[0]=1;
        for (int i = 1; i < n; i++) {
            res[i]= res[i-1]*nums[i-1];
        }
        int[] R=new int[n];
        R[n-1]=1;
        for (int i = n-2; i >=0 ; i--) {
            R[i]=R[i+1]*nums[i+1];
        }
        for (int i = 0; i < n; i++) {
            res[i]=res[i]*R[i];
        }
        return res;
    }
}
